package com.smart.file.utils;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 文件工具类
 *
 * @author wf
 * @since 2023-04-04 17:17:50
 */
public class PdfUtils {

    /**
     * 判断是否有授权文件 如果没有则会认为是试用版，转换的文件会有水印
     */
    public static boolean getLicense() {
        try {
            InputStream is = PdfUtils.class.getClassLoader().getResourceAsStream("static/license/license.xml");
            License license = new License();
            license.setLicense(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * Word转PDF操作
     */
    public static void docToPdf(InputStream inputStream, OutputStream outputStreamPdf) {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return;
        }
        try {
            Document doc = new Document(inputStream);
            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            doc.save(outputStreamPdf, SaveFormat.PDF);
            outputStreamPdf.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 给pdf文件添加图片水印
     *
     * @param inputStream 要加水印的原pdf文件
     */
    public static void addPdfImgMark(InputStream inputStream, HttpServletResponse response) {
        PdfStamper stamp = null;
        PdfReader reader;
        try {
            reader = new PdfReader(inputStream);
            stamp = new PdfStamper(reader, response.getOutputStream());
            int numberOfPages = reader.getNumberOfPages();

            ClassPathResource classPathResource = new ClassPathResource("/static/img/a4-logo.png");
            // 水印图象的路径 水印一般为gif或者png的，这样可设置透明度
            Image img = Image.getInstance(classPathResource.getURL());
            img.setAbsolutePosition(0, 0);
            // 水印的宽度
            float plainWidth = img.getPlainWidth();
            for (int i = 1; i <= numberOfPages; i++) {
                // pdf当前页的宽高
                Rectangle pageSizeWithRotation = reader.getPageSizeWithRotation(i);
                float width = pageSizeWithRotation.getWidth();
                // 缩放系数
                float v = width / plainWidth;
                img.scalePercent(v * 100);
                PdfContentByte under = stamp.getOverContent(i);
                under.addImage(img);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != stamp) {
                    stamp.close();// 关闭
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
